package com.zmn.oms.services.impl.master;

import com.google.common.collect.Lists;
import com.zmn.oms.model.entity.master.OrderMaster;
import com.zmn.oms.persistence.interfaces.master.OrderMasterDao;
import com.zmn.oms.services.interfaces.master.OrderMasterService;
import com.zmn.oms.services.interfaces.orderencrypt.OrderMasterEncryptService;
import io.shardingsphere.api.HintManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：工单工程师
 * @author lujia
 * @date 2018/11/05 9:07
 */
@Service
@Slf4j
public class OrderMasterServiceImpl implements OrderMasterService {

    @Resource
    private OrderMasterDao orderMasterDao;
    @Autowired
    private OrderMasterEncryptService orderMasterEncryptService;

    @Override
    public List<OrderMaster> listMasterByWorkId(Long orderId, Long workId) {
        List<OrderMaster> orderMasters = orderMasterDao.listMasterByKey(orderId, workId, null);
        orderMasterEncryptService.decryptOrderMaster(orderMasters);// 解密工程师手机号
        return orderMasters;
    }

    @Override
    public List<OrderMaster> listMasterByWorkId(Long orderId, Long workId, Integer type) {
        List<OrderMaster> orderMasters = orderMasterDao.listMasterByKey(orderId, workId, type);
        orderMasterEncryptService.decryptOrderMaster(orderMasters);// 解密工程师手机号
        return orderMasters;
    }

    @Override
    public List<OrderMaster> listMasterByWorkIdSrcMaster(Long orderId, Long workId, Integer type) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            List<OrderMaster> orderMasters = orderMasterDao.listMasterByKey(orderId, workId, type);
            orderMasterEncryptService.decryptOrderMaster(orderMasters);// 解密工程师手机号
            return orderMasters;
        }
    }


    @Override
    public List<OrderMaster> listMasterByOrderIdList(List<Long> orderIdList) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            List<OrderMaster> orderMasters = orderMasterDao.listMasterByOrderIdList(orderIdList);
            orderMasterEncryptService.decryptOrderMaster(orderMasters);// 解密工程师手机号
            return orderMasters;
        }
    }

    @Override
    public List<OrderMaster> listMasterByMasterIdList(Long orderId, Long workId, List<Integer> masterIdList) {
        List<OrderMaster> orderMasters = orderMasterDao.listMasterByMasterIdList(orderId, workId, masterIdList);
        orderMasterEncryptService.decryptOrderMaster(orderMasters);// 解密工程师手机号
        return orderMasters;
    }

    @Override
    public int insertOrderMaster(OrderMaster orderMaster) {
        List<OrderMaster> encryptOrderMaster = orderMasterEncryptService.getEncryptOrderMaster(Lists.newArrayList(orderMaster));// 加密工程师手机号
        if (Objects.nonNull(orderMaster)) {
            return orderMasterDao.insert(encryptOrderMaster.get(0));
        } else {
            log.error("orderMaster插入错误----");
            return 0;
        }
    }

    @Override
    public int insertAll(List<OrderMaster> orderMasters) {
        List<OrderMaster> encryptOrderMaster = orderMasterEncryptService.getEncryptOrderMaster(orderMasters);// 加密工程师手机号
        return orderMasterDao.insertAll(encryptOrderMaster);
    }

    @Override
    public int deleteMaster(Long orderId, Long workId, Integer type) {
        return orderMasterDao.deleteMaster(orderId, workId, type);
    }

    @Override
    public int deleteMasterByMasterId(Long orderId, Long workId, Integer masterId) {
        return orderMasterDao.deleteMasterByMasterId(orderId, workId, masterId);
    }

    @Override
    public int deleteMasterByMasterIdList(Long orderId, Long workId, Integer type, List<Integer> masterIdList) {
        return orderMasterDao.deleteMasterByMasterIdList(orderId, workId, type, masterIdList);
    }

    @Override
    public int updateMasterShare(OrderMaster master) {
        return orderMasterDao.updateMasterShare(master);
    }
}
